package com.kgc.springboot.aviation.controller.lvwenjia;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;

import com.kgc.springboot.aviation.pojo.*;
import com.kgc.springboot.aviation.service.lvwenjia.ClauseService;
import com.kgc.springboot.aviation.service.lvwenjia.LawsServices;
import com.kgc.springboot.aviation.service.lvwenjia.MenuService;
import com.kgc.springboot.aviation.service.lvwenjia.OpinionService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-12-29 15:55
 */
@Controller
public class ClauseController {
    @Resource
    ClauseService clauseService;
    @Resource
    LawsServices lawsServices;
    @Resource
    OpinionService opinionService;
    @Resource
    MenuService menuService;



    @RequestMapping("/clause")
    public String clause(Model model,
                         @RequestParam( value = "lawsId", required = false, defaultValue = "0")Integer lawsId,
                         @RequestParam( value = "clauseId", required = false, defaultValue = "")String clauseId,
                         @RequestParam( value = "keyword", required = false, defaultValue = "")String keyword,
                         @RequestParam( value = "pageNum", required = false, defaultValue = "1")int pageNum) {
        PageInfo<Clause> pageInfo = clauseService.selectClause(lawsId, clauseId, keyword, pageNum);
        List<Laws> laws = lawsServices.getLawsList("");
        model.addAttribute("lawsId",lawsId);
        model.addAttribute("clauseId",clauseId);
        model.addAttribute("keyword",keyword);
        model.addAttribute("laws",laws);
        model.addAttribute("pageInfo",pageInfo);
        return "clause";
    }

    @RequestMapping("/addClause")
    public String addCluse(Model model){
        List<Laws> laws = lawsServices.getLawsList("");
        List<Menu> menuList = menuService.getMenuList();
        model.addAttribute("laws",laws);
        model.addAttribute("menuList",menuList);
        return "addclause";
    }


    @RequestMapping("/delClause")
    public void delClause(HttpServletResponse response,@RequestParam("cid")Integer id){
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        int i = clauseService.delClauseById(id);
        if(i>0){
            out.print("<script>alert('删除成功！');location.href='/clause';</script>");
        }else{
            out.print("<script>alert('删除失败！')</script>");
        }
    }

    @RequestMapping("insertClause")
    public void insertClause(HttpServletResponse response,Clause clause){
        System.out.println(clause.toString());
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        int i = clauseService.insertClause(clause);
        if(i>0){
            out.print("<script>alert('添加成功！');location.href='/clause';</script>");
        }else{
            out.print("<script>alert('添加失败！')</script>");
        }
    }
    @RequestMapping("doUpdate")
    public String doUpdate(Integer id,Model model){
        System.out.println(id);
        System.out.println("进入");
        List<Laws> laws = lawsServices.getLawsList("");
        Clause clause = clauseService.selectClauseById(id);
        List<Menu> menuList = menuService.getMenuList();
        model.addAttribute("laws",laws);
        model.addAttribute("clause",clause);
        model.addAttribute("menuList",menuList);
        return "doUpdate";
    }

    @RequestMapping("updateClause")
    public void updateClause(HttpServletResponse response,Clause clause){
        System.out.println(clause.toString());
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        int i = clauseService.updateClause(clause);
        if(i>0){
            out.print("<script>alert('修改成功！');location.href='/clause';</script>");
        }else{
            out.print("<script>alert('修改失败！')</script>");
        }
    }

    @RequestMapping("/fankui")
    public void fankui(Opinion opinion, HttpServletResponse response, HttpSession session){
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = null;
        Users users = (Users) session.getAttribute("users");
        if (users!=null){
            try {
                out = response.getWriter();
            } catch (IOException e) {
                e.printStackTrace();
            }
            opinion.setOpinionDate(new Date());
            opinion.setUserId(users.getId());
            System.out.println(opinion.toString());
            int i = opinionService.insertOpinion(opinion);
            if(i>0){
                out.print("<script>alert('添加成功！');location.href='/ooo';</script>");
            }else{
                out.print("<script>alert('添加失败！')</script>");
            }
        }

    }

    @RequestMapping("/fankui2")
    public String fankui2(Model model){
        List<Opinion> opinionList = opinionService.getOpinionList();
        model.addAttribute("opinionList",opinionList);
        return "feedback2";
    }

    @RequestMapping("/chaxunfankui")
    @ResponseBody
    public String chaxunfankui(@RequestParam( value = "id", required = false)Integer id){
        Opinion opinion = opinionService.selectOpinionById(id);
        ObjectMapper mapper = new ObjectMapper();
        String json="";
        try {
            json = mapper.writeValueAsString(opinion);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        System.out.println(json.toString());
        System.out.println(opinion.toString());
        return json;
    }

}
